package com.atsmart.app.sql;

import java.util.ArrayList;
import java.util.List;

import com.atsmart.app.model.TemplateInfo;
import com.atsmart.app.model.TemplateListInfo;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

/**
 * 
s * 2013-6-13 上午10:09:58
 * TODO
 */
public class TempFromTaskSql {
	
	private static final String TAG = "TempSqlHelper";
	
	private SQLiteDatabase db = null;

	/**
	 * 打开数据库
	 */
	public void open(SqlHelper dh) {
		// dh = new SqlHelper(null, "atsmart", null,SqlHelper.getDBVersion());
		db = dh.getWritableDatabase();
	}
	
	
	public synchronized void addTemplateInfo(TemplateInfo at,int task_id) {
		if (at == null) {
			return;
		}
		String strSql = "insert or replace into tem_info (tem_id,name,desc,type,task_id) values(?,?,?,?,?);";
		try {
			SQLiteStatement stm = db.compileStatement(strSql);
			stm.bindLong(1, at.getId());
			stm.bindString(2, at.getName()==null?"":at.getName());
			stm.bindString(3, at.getDesc()==null?"":at.getDesc());
			stm.bindLong(4,at.getUpdate_type());
			stm.bindLong(5,task_id);
			stm.execute();
			stm.clearBindings();
			stm.close();
		} catch (Exception e) {
			// will not happen
		}
	}
	
	 
	
	public TemplateListInfo getTemplateInfoByTaskId(int task_id) {
		String sql = "";
		sql = " select _id,tem_id,name,desc,type,task_id from tem_info where task_id="
				+ task_id;

		Cursor cursor = db.rawQuery(sql, null);
		TemplateInfo historie = null;
		List<TemplateInfo> nums = null;
		TemplateListInfo t = null;
		try {
			if (cursor != null && cursor.moveToFirst()) {
				nums = new ArrayList<TemplateInfo>();
				t = new TemplateListInfo();
				for (int i = 0; i < cursor.getCount(); i++) {
					historie = new TemplateInfo();
					historie.setId(cursor.getInt(1));
					historie.setName(cursor.getString(2));
					historie.setDesc(cursor.getString(3));
					historie.setUpdate_type(cursor.getInt(4));
					nums.add(historie);
					cursor.moveToNext();
				}
				t.setTemplates(nums);
			}
		} catch (Exception e) {
			return null;
		} finally {
			cursor.close();
		}
		return t;
	}
	
	public TemplateListInfo getTemp() {
		String sql = "";
		sql = " select _id,tem_id,name,desc,type,task_id from tem_info";

		Cursor cursor = db.rawQuery(sql, null);
		TemplateInfo historie = null;
		List<TemplateInfo> nums = null;
		TemplateListInfo t = null;
		try {
			if (cursor != null && cursor.moveToFirst()) {
				nums = new ArrayList<TemplateInfo>();
				t = new TemplateListInfo();
				for (int i = 0; i < cursor.getCount(); i++) {
					historie = new TemplateInfo();
					historie.setId(cursor.getInt(1));
					historie.setName(cursor.getString(2));
					historie.setDesc(cursor.getString(3));
					historie.setUpdate_type(cursor.getInt(4));
					nums.add(historie);
					cursor.moveToNext();
				}
				t.setTemplates(nums);
			}
		} catch (Exception e) {
			return null;
		} finally {
			cursor.close();
		}
		return t;
	}
	
	public TemplateListInfo getTemplateInfoById(int tem_id) {
		String sql ="";
		sql = " select _id,tem_id,name,desc,type,task_id from tem_info where tem_id="+tem_id;
		 
		Cursor cursor = db.rawQuery(sql, null);
		TemplateInfo historie = null;
		List<TemplateInfo> nums = null;
		TemplateListInfo t = null;
		try {
			if (cursor != null && cursor.moveToFirst()) {
				nums = new ArrayList<TemplateInfo>();
				t = new TemplateListInfo();
				for (int i = 0; i < cursor.getCount(); i++) {
					historie = new TemplateInfo();
					historie.setId(cursor.getInt(1));
					historie.setName(cursor.getString(2));
					historie.setDesc(cursor.getString(3));
					historie.setUpdate_type(cursor.getInt(4));
				
					nums.add(historie);
					cursor.moveToNext();
				}
				t.setTemplates(nums);
			}
		} catch (Exception e) {
			return null;
		} finally {
			cursor.close();
		}
		return t;
	}
	

	public int getCount(int task_id) {
		String sql ="select * from tem_info where task_id="+task_id;
		Cursor cursor = db.rawQuery(sql, null);
		try {
			if (cursor != null && cursor.moveToFirst()) {
				return cursor.getCount();
			}
		} catch (Exception e) {
			return 0;
		} finally {
			cursor.close();
		}
		return 0;
	}
	
	
	public void updata(int type, int tem_id, int task_id){
//		tem_id,name,desc,type,task_id
		String sql = "updata tem_info set type=" + type + " where tem_id=" + tem_id +" and task_id=" + task_id;
		db.equals(sql);
	}
	
	public void updateTemplate(int task_id, int num , int temId) {
		ContentValues cv = new ContentValues();
		cv.put("tem_id", num);
		db.update("tem_info", cv, " tem_id=" + task_id + " and tem_id=" + temId, null);
	}
	
	public void deleteAttribute(int tem_id,int task_id) {
		// db.delete("position_attribute", null, null);
		db.execSQL("delete from tem_info where tem_id="+tem_id+" and task_id="+task_id);
	}
//	public void deleteTem(int tem_id,int type) {
//		// db.delete("position_attribute", null, null);
//		db.execSQL("delete from v where tem_id="+tem_id +" and type="+type);
//	}
	public void delete() {
		// db.delete("position_attribute", null, null);
		db.execSQL("delete from tem_info ");
	}
}
